home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / MultiSession 1.04 Source / Core 27⁄June⁄1993 / CApplication.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-05  |  10.9 KB  |  425 lines  |  [TEXT/KAHL]

  1. /* CApplication.c */
  2.  
  3. #define COMPILING_CAPPLICATION_C
  4. #include "CApplication.h"
  5. #include "CMyScrap.h"
  6. #include "MenuController.h"
  7. #include "EventLoop.h"
  8. #include "CMyDocument.h"
  9. #include "CAboutWindow.h"
  10. #include "Compatibility.h"
  11. #include "StandardFile.h"
  12. #include "CSack.h"
  13. #include "Alert.h"
  14. #include "Memory.h"
  15. #include "File.h"
  16. #include "CMyApplication.h"
  17. #include "WatchCursor.h"
  18.  
  19.  
  20. #define NotEnoughMemoryID (130L*65536L + 2)
  21.  
  22. static    CApplication*    CApplication::GlobalApplication = NIL; /* so high level event handers can reach it */
  23.  
  24.  
  25. CMyApplication*        Application;
  26.  
  27.  
  28. /* */            CApplication::CApplication()
  29.     {
  30.         MyBoolean        NoErr;
  31.         CSack*            Temp;
  32.  
  33.         /* initializing instance variables */
  34.         Temp = new CSack;
  35.         Temp->ISack(sizeof(CDocument*),256);
  36.         ListOfDocuments = Temp;
  37.         AboutWindow = NIL;
  38.  
  39.         GlobalApplication = this;
  40.         /* installing apple event handlers */
  41.         NoErr = True;
  42.         NoErr = NoErr && (AEInstallEventHandler(kCoreEventClass,
  43.             kAEOpenApplication,&MyHandleOApp,0,False) == noErr);
  44.         NoErr = NoErr && (AEInstallEventHandler(kCoreEventClass,
  45.             kAEOpenDocuments,&MyHandleODoc,0,False) == noErr);
  46.         NoErr = NoErr && (AEInstallEventHandler(kCoreEventClass,
  47.             kAEPrintDocuments,&MyHandlePDoc,0,False) == noErr);
  48.         NoErr = NoErr && (AEInstallEventHandler(kCoreEventClass,
  49.             kAEQuitApplication,&MyHandleQuit,0,False) == noErr);
  50.         if (!NoErr)
  51.             {
  52.                 PRERR(ForceAbort,"Unable to install Apple Event Handlers!");
  53.             }
  54.  
  55.         IWindow(LongPointOf(-32767,-32767),LongPointOf(-32766,-32766),
  56.             ModelessWindow,NoGrowable,NoZoomable);
  57.         SelectWindow(MyGrafPtr);
  58.     }
  59.  
  60.  
  61. /* */            CApplication::~CApplication()
  62.     {
  63.         delete ListOfDocuments;
  64.         ERROR(AboutWindow!=NIL,PRERR(ForceAbort,"About Window wasn't deleted."));
  65.     }
  66.  
  67.  
  68. /* function to check to see that all required parameters have been gotten */
  69. static    OSErr        CApplication::MyGotRequiredParams(AppleEvent* theAppleEvent)
  70.     {
  71.         DescType        ReturnedType;
  72.         Size                ActualSize;
  73.         OSErr                Error;
  74.  
  75.         Error = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
  76.             &ReturnedType,NIL,0,&ActualSize);
  77.         if (Error == errAEDescNotFound)
  78.             {
  79.                 return noErr;  /* we got all the params, since no more were found */
  80.             }
  81.          else
  82.             {
  83.                 if (Error == noErr)
  84.                     {
  85.                         return errAEEventNotHandled;  /* missed some, so it failed */
  86.                     }
  87.                  else
  88.                     {
  89.                         return Error;  /* AEGetAttributePtr failed, so we return why */
  90.                     }
  91.             }
  92.     }
  93.  
  94.  
  95. /* handler for open application--presents an untitled document */
  96. static    pascal    OSErr    CApplication::MyHandleOApp(AppleEvent* theAppleEvent,
  97.                                                 AppleEvent* reply, long handlerRefcon)
  98.     {
  99.         OSErr        Error;
  100.  
  101.         Error = MyGotRequiredParams(theAppleEvent);
  102.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleOApp error."));
  103.         if (Error != noErr)
  104.             {
  105.                 return Error;
  106.             }
  107.         GlobalApplication->DispatchOpenApplication();
  108.         return noErr;
  109.     }
  110.  
  111. /* handler for open documents */
  112. static    pascal    OSErr    CApplication::MyHandleODoc(AppleEvent* theAppleEvent,
  113.                                                 AppleEvent* reply, long handlerRefcon)
  114.     {
  115.         OSErr                Error;
  116.         FSSpec            MyFSS;  /* place to put file info */
  117.         long                Index,ItemsInList;
  118.         AEDescList    DocList;
  119.         Size                ActualSize;
  120.         AEKeyword        Keywd;
  121.         DescType        ReturnedType;
  122.  
  123.         /* get the direct parameter--a descriptor list--and put it into DocList */
  124.         Error = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &DocList);
  125.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleODoc error."));
  126.         if (Error != noErr) return Error;
  127.         /* check for missing required parameters */
  128.         Error = MyGotRequiredParams(theAppleEvent);
  129.         if (Error != noErr) return Error;
  130.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleODoc error."));
  131.         /* count the number of descriptor records in the list */
  132.         Error = AECountItems(&DocList,&ItemsInList);
  133.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleODoc error."));
  134.         /* now get each descriptor record from the list, coerce the returned data */
  135.         /* to an FSSpec record, and open the associated file */
  136.         if (WeAreActiveApplication)
  137.             {
  138.                 StartTimeConsumingOperation();
  139.             }
  140.         for (Index=1; Index <= ItemsInList; Index += 1)
  141.             {
  142.                 Error = AEGetNthPtr(&DocList,Index,typeFSS,&Keywd,&ReturnedType,
  143.                     (void*)&MyFSS,sizeof(FSSpec),&ActualSize);
  144.                 ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleODoc error."));
  145.                 if (Error == noErr)
  146.                     {
  147.                         GlobalApplication->DispatchOpenDocument(&MyFSS);
  148.                     }
  149.                 if (WeAreActiveApplication)
  150.                     {
  151.                         CheckCursor();
  152.                     }
  153.             }
  154.         if (WeAreActiveApplication)
  155.             {
  156.                 EndTimeConsumingOperation();
  157.             }
  158.         Error = AEDisposeDesc(&DocList);
  159.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleODoc error."));
  160.         return Error;
  161.     }
  162.  
  163. /* handler for print documents */
  164. static    pascal    OSErr    CApplication::MyHandlePDoc(AppleEvent* theAppleEvent,
  165.                                                 AppleEvent* reply, long handlerRefcon)
  166.     {
  167.         OSErr                Error;
  168.         FSSpec            MyFSS;  /* place to put file info */
  169.         long                Index,ItemsInList;
  170.         AEDescList    DocList;
  171.         Size                ActualSize;
  172.         AEKeyword        Keywd;
  173.         DescType        ReturnedType;
  174.  
  175.         /* get the direct parameter--a descriptor list--and put it into DocList */
  176.         Error = AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &DocList);
  177.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandlePDoc error."));
  178.         if (Error != noErr) return Error;
  179.         /* check for missing required parameters */
  180.         Error = MyGotRequiredParams(theAppleEvent);
  181.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandlePDoc error."));
  182.         if (Error != noErr) return Error;
  183.         /* count the number of descriptor records in the list */
  184.         Error = AECountItems(&DocList,&ItemsInList);
  185.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandlePDoc error."));
  186.         /* now get each descriptor record from the list, coerce the returned data */
  187.         /* to an FSSpec record, and open the associated file */
  188.         for (Index=1; Index <= ItemsInList; Index += 1)
  189.             {
  190.                 Error = AEGetNthPtr(&DocList,Index,typeFSS,&Keywd,&ReturnedType,
  191.                     (void*)&MyFSS,sizeof(FSSpec),&ActualSize);
  192.                 ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandlePDoc error."));
  193.                 if (Error == noErr)
  194.                     {
  195.                         GlobalApplication->DispatchPrintDocument(&MyFSS);
  196.                     }
  197.             }
  198.         Error = AEDisposeDesc(&DocList);
  199.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandlePDoc error."));
  200.         return Error;
  201.     }
  202.  
  203. /* handle a quit event */
  204. static    pascal    OSErr    CApplication::MyHandleQuit(AppleEvent* theAppleEvent,
  205.                                                 AppleEvent* reply, long handlerRefcon)
  206.     {
  207. /*    MyBoolean    UserCanceled; */
  208.         OSErr            Error;
  209.  
  210.         /* check for missing required parameters */
  211.         Error = MyGotRequiredParams(theAppleEvent);
  212.         ERROR(Error!=noErr,PRERR(AllowResume,"CApplication::MyHandleQuit error."));
  213.         if (Error != noErr) return Error;
  214.         /* UserCanceled = */ GlobalApplication->DispatchQuit();
  215. /*    if (UserCanceled)
  216.             {
  217.                 return userCanceledErr;
  218.             }
  219.          else
  220.             {
  221.                 return noErr;
  222.             }    */
  223.         return noErr;
  224.     }
  225.  
  226.  
  227. /* accept and process high level events */
  228. void        CApplication::ReceiveHighLevelEvent(EventRecord* Event)
  229.     {
  230.         OSErr        Error;
  231.  
  232.         Error = AEProcessAppleEvent(Event);
  233.         ERROR(Error != noErr,PRERR(AllowResume,
  234.             "CApplication::ReceiveHighLevelEvent AEProcessAppleEvent error."));
  235.     }
  236.  
  237.  
  238. void        CApplication::RegisterDocument(CDocument* TheDocument)
  239.     {
  240.         ListOfDocuments->PushElement(&TheDocument);
  241.     }
  242.  
  243.  
  244. void        CApplication::DeregisterDocument(CDocument* TheDocument)
  245.     {
  246.         ListOfDocuments->KillElement(&TheDocument);
  247.     }
  248.  
  249.  
  250. void        CApplication::InitiateQuit(void)
  251.     {
  252.         if (AboutWindow != NIL)
  253.             {
  254.                 delete AboutWindow;
  255.                 AboutWindow = NIL;
  256.             }
  257.         while (ListOfDocuments->NumElements() != 0)
  258.             {
  259.                 CDocument*    TheDocument;
  260.  
  261.                 ListOfDocuments->ResetScan();
  262.                 ListOfDocuments->GetNext(&TheDocument);
  263.                 if (!TheDocument->GoAway())
  264.                     {
  265.                         return; /* quit operation was aborted */
  266.                     }
  267.             }
  268.         /* quit operation was not aborted: all documents closed successfully */
  269.         QuitPending = True;
  270.     }
  271.  
  272.  
  273. /* if the active window couldn't handle a command, it gets sent here */
  274. MyBoolean    CApplication::DoMenuCommand(ushort MenuCommand)
  275.     {
  276.         switch (MenuCommand)
  277.             {
  278.                 case mFileNew:
  279.                     DoMenuNew();
  280.                     return True;
  281.                 case mFileOpen:
  282.                     DoMenuOpen();
  283.                     return True;
  284.                 case mAppleAbout:
  285.                     DoAboutWindow(False);
  286.                     return True;
  287.                 case mFileQuit:
  288.                     InitiateQuit();
  289.                     return True;
  290.                 default:
  291.                     return False;
  292.             }
  293.     }
  294.  
  295.  
  296. /* create a new document */
  297. void        CApplication::DispatchOpenApplication(void)
  298.     {
  299.         DoMenuNew();
  300.     }
  301.  
  302.  
  303. /* open a document using specified file */
  304. void        CApplication::DispatchOpenDocument(FSSpec* TheFSSpec)
  305.     {
  306.         CMyDocument*    ADoc;
  307.  
  308.         if (!FirstMemCacheValid())
  309.             {
  310.                 AlertError(NotEnoughMemoryID,NIL);
  311.                 return;
  312.             }
  313.         ADoc = new CMyDocument;  /* create the document */
  314.         ADoc->DoOpenFile(TheFSSpec);  /* make it link to a file */
  315.     }
  316.  
  317.  
  318. /* open the document, print it, and close it */
  319. void        CApplication::DispatchPrintDocument(FSSpec* TheFSSpec)
  320.     {
  321.         CMyDocument*    ADoc;
  322.  
  323.         if (!FirstMemCacheValid())
  324.             {
  325.                 AlertError(NotEnoughMemoryID,NIL);
  326.                 return;
  327.             }
  328.         ADoc = new CMyDocument;  /* create the document */
  329.         ADoc->DoOpenFile(TheFSSpec);  /* make it link to a file */
  330.         ADoc->DoPrint();  /* cause it to print itself */
  331.         ADoc->GoAway();  /* cause it to go away */
  332.     }
  333.  
  334.  
  335. /* tell document tracker to close all documents.  If user aborts, it return true, otherwise */
  336. /* it return false */
  337. void        CApplication::DispatchQuit(void)
  338.     {
  339.         InitiateQuit(); /* indicate that we should quit when everything closes */
  340.     }
  341.  
  342.  
  343. void        CApplication::DoAboutWindow(MyBoolean AutoFlag)
  344.     {
  345.         CAboutWindow*        TempAboutWindow;
  346.  
  347.         if (AboutWindow != NIL)
  348.             {
  349.                 AboutWindow->BecomeActiveWindow();
  350.             }
  351.          else
  352.             {
  353.                 if (!FirstMemCacheValid())
  354.                     {
  355.                         AlertError(NotEnoughMemoryID,NIL);
  356.                         return;
  357.                     }
  358.                 TempAboutWindow = new CAboutWindow;
  359.                 TempAboutWindow->IAboutWindow(AutoFlag);
  360.                 AboutWindow = TempAboutWindow;
  361.             }
  362.     }
  363.  
  364.  
  365. /* enable items the application can handle */
  366. void        CApplication::EnableMenuItems(void)
  367.     {
  368.         MyEnableItem(mFileNew);
  369.         MyEnableItem(mFileOpen);
  370.         MyEnableItem(mAppleAbout);
  371.         MyEnableItem(mFileQuit);
  372.     }
  373.  
  374.  
  375. void        CApplication::InitMenuBar(void)
  376.     {
  377.         EXECUTE(PRERR(ForceAbort,"CApplication::InitMenuBar isn't overridden"));
  378.     }
  379.  
  380.  
  381. void        CApplication::DoMenuNew(void)
  382.     {
  383.         CMyDocument*    ADoc;
  384.  
  385.         if (!FirstMemCacheValid())
  386.             {
  387.                 AlertError(NotEnoughMemoryID,NIL);
  388.                 return;
  389.             }
  390.         ADoc = new CMyDocument;
  391.         ADoc->DoNewFile();
  392.     }
  393.  
  394.  
  395. /* open a file using the standard file things */
  396. void        CApplication::DoMenuOpen(void)
  397.     {
  398.         FSSpec            FileInfo;
  399.         OSType            TypeList[NUMFILETYPES];
  400.         short                NumTypes;
  401.         pascal Boolean (*FileFilter)(CInfoPBRec* pb);
  402.  
  403.         if (!FirstMemCacheValid())
  404.             {
  405.                 AlertError(NotEnoughMemoryID,NIL);
  406.                 return;
  407.             }
  408.         ConstructFileTypeTable(TypeList,&NumTypes,&FileFilter);
  409.         if (FGetFile(&FileInfo,FileFilter,TypeList,NumTypes))
  410.             {
  411.                 DispatchOpenDocument(&FileInfo);
  412.             }
  413.     }
  414.  
  415.  
  416. void        CApplication::ConstructFileTypeTable(OSType Table[NUMFILETYPES], short* NumTypes,
  417.                     pascal Boolean (**FileFilter)(CInfoPBRec* pb))
  418.     {
  419.         OSType        LocalList[NUMFILETYPES] = FILETYPELIST;
  420.  
  421.         MemCpy((char*)Table,(char*)LocalList,NUMFILETYPES * sizeof(OSType));
  422.         *NumTypes = NUMFILETYPES;
  423.         *FileFilter = NIL;
  424.     }
  425.